package com.fyoung.coordinatorlayoutdemo;

import android.graphics.Color;
import android.support.v7.widget.CardView;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.fyoung.coordinatorlayoutdemo.listener.OnDragVHListener;
import com.fyoung.coordinatorlayoutdemo.listener.OnItemMoveListener;

import java.util.List;

/**
 * Created by Administrator on 2017/6/13.
 */

public class LifeAdapter extends RecyclerView.Adapter<LifeAdapter.ViewHolder> implements View.OnClickListener,View.OnLongClickListener,OnItemMoveListener{

    private String color[] = {"#1dcdef", "#FFFF40", "#FF4081"};
    List<ItemData> list;

    private RecyclerViewOnItemClickListener onItemClickListener;
    private RecyclerViewOnItemLongClickListener onItemLongClickListener;


    public LifeAdapter(List<ItemData> list) {
        this.list = list;
    }

    @Override
    public LifeAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View root = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_life_layout,parent,false);
        ViewHolder vh = new ViewHolder(root);
        root.setOnClickListener(this);
        root.setOnLongClickListener(this);
        return vh;
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
        holder.title.setText(list.get(position).getTitle());
        //设置Tag
        holder.content.setText(list.get(position).getContent());
        holder.cardView.setCardBackgroundColor(Color.parseColor(color[position % color.length]));
        holder.root.setTag(position);
    }


    @Override
    public int getItemCount() {
        return list.size();
    }


    public class ViewHolder extends RecyclerView.ViewHolder implements OnDragVHListener {

        private TextView title, content;
        private View root;
        private CardView cardView;

        public ViewHolder(View itemView) {
            super(itemView);
            this.root = itemView;
            title = (TextView) itemView.findViewById(R.id.tv_title);
            content = (TextView) itemView.findViewById(R.id.tv_content);
            cardView = (CardView) itemView.findViewById(R.id.card);
        }

        @Override
        public void onItemSelected() {
            //开始推拽排序,可以设置被拖拽Item的背景
            //itemView.setBackgroundResource();
        }

        @Override
        public void onItemFinish() {
            //推拽排序结束,也可以设置被拖拽Item的背景
        }
    }


    @Override
    public void onClick(View v) {
        if (onItemClickListener != null) {
            //注意这里使用getTag方法获取数据
            onItemClickListener.onItemClickListener(v, (Integer) v.getTag());
        }
    }

    @Override
    public boolean onLongClick(View v) {
        return onItemLongClickListener != null && onItemLongClickListener.onItemLongClickListener(v, (Integer) v.getTag());
    }

    @Override
    public void onItemMove(int fromPosition, int toPosition) {
        ItemData data = list.get(fromPosition);
        list.remove(fromPosition);
        list.add(toPosition, data);
        notifyItemMoved(fromPosition, toPosition);
    }

    /*设置点击事件*/
    public void setRecyclerViewOnItemClickListener(RecyclerViewOnItemClickListener onItemClickListener) {
        this.onItemClickListener = onItemClickListener;
    }

    /*设置长按事件*/
    public void setOnItemLongClickListener(RecyclerViewOnItemLongClickListener onItemLongClickListener) {
        this.onItemLongClickListener = onItemLongClickListener;
    }

    public interface RecyclerViewOnItemClickListener {

        void onItemClickListener(View view, int position);

    }

    public interface RecyclerViewOnItemLongClickListener {

        boolean onItemLongClickListener(View view, int position);

    }

}
